﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

using CMSApplication.Models;
using System.Drawing;
using System.Drawing.Imaging;
using CMSApplication.Logic;

namespace CMSApplication.Controllers
{
    public class ResourcesController : Controller
    {
        private ResourceManager manager;
        //
        // GET: /Resources/

        public ActionResult Index()
        {    
            return View(manager.GetAllResources());
        }

        /// <summary>
        /// call this to populate lists by mimeType (css, javascript, images, video, audio) the more info the better
        /// </summary>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public ActionResult Index(string contentType)
        {             
            return View(manager.GetResourcesByContentType(contentType));
        }

        //
        // GET: /Resources/Retrieve/5

        public ActionResult Retrieve(int id)
        {
            var resource = manager.GetResource(id);

                // if resource is an image
                if (resource.MimeType.Contains("image"))
                {
                    //string imgFormat = resource.MimeType.Substring(resource.MimeType.IndexOf("/") + 1);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        byte[] image = resource.Stream;
                        ms.Write(image, 0, image.Length);
                        Bitmap bitmap = new Bitmap(ms);
                        Response.ContentType = resource.MimeType;
                        bitmap.Save(Response.OutputStream, bitmap.RawFormat);
                    }
                }

                else
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        byte[] file = resource.Stream;
                        ms.Write(file, 0, file.Length);
                        Response.ContentType = resource.MimeType;
                        ms.WriteTo(Response.OutputStream);
                    }
                }
       
            return View();
        }

        //
        // GET: /Resources/Create

        public ActionResult Create()
        {
            return PartialView();
        } 
            
        //
        // POST: /Resources/Create

        [HttpPost]
        public ActionResult Upload(HttpPostedFile file, int siteId)
        {
            if (file != null && file.ContentLength > 0)
            {
                if (manager.Save(file, siteId))
                {
                    ViewBag.Message = "Successfully uploaded file.";
                    return View();
                }
                else
                {
                    ViewBag.Message = "Sorry! There was a problem uploading your file. Please try again.";
                    return RedirectToAction(Request.UrlReferrer.PathAndQuery);      
                }
                     
            }
            else
            {
                return RedirectToAction(Request.UrlReferrer.PathAndQuery);
            }
            
        }
        

        ////
        //// GET: /Resources/Delete/5
 
        //public ActionResult Delete(int id)
        //{
        //    return View();
        //}

        ////
        //// POST: /Resources/Delete/5

        //[HttpPost]
        //public ActionResult Delete(int id, FormCollection collection)
        //{
        //    try
        //    {
        //        // TODO: Add delete logic here
 
        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}
    }
}
